<template>
  <!-- 企业许可证 -->
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="100px">
      <el-form-item label="发证时间" prop="permitIssueDate">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="许可证名称" prop="permitIssueName">
        <el-select v-model="queryParams.permitIssueName" placeholder="请选择许可证名称">
          <el-option
            v-for="dict in dict.type.bhxq_enterprise_permit_issue"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="许可证编号" prop="permitIssueCode">
        <el-input
          v-model="queryParams.permitIssueCode"
          clearable
          placeholder="请输入许可证编号"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--<el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增企业营运证件</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="许可证名称" align="center" prop="permitIssueName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bhxq_enterprise_permit_issue" :value="scope.row.permitIssueName" />
        </template>
      </el-table-column>
      <el-table-column label="许可证编号" align="center" prop="permitIssueCode" />
      <el-table-column label="许可证发证机关" align="center" prop="permitIssueOrg" />
      <el-table-column label="许可证初次发证日期" align="center" prop="permitIssueDate" />
      <el-table-column label="许可证本次发证日期" align="center" prop="permitStartDate" />
      <el-table-column label="许可证有效截止日期" align="center" prop="permitEndDate" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业许可证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="许可证名称" prop="permitIssueName">
              <el-select v-model="form.permitIssueName" placeholder="请选择许可证名称">
                <el-option
                  v-for="dict in dict.type.bhxq_enterprise_permit_issue"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="许可证编号" prop="permitIssueCode">
              <el-input v-model="form.permitIssueCode" placeholder="请输入许可证编号" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="许可证发证机关" prop="permitIssueOrg">
              <el-input v-model="form.permitIssueOrg" placeholder="请输入许可证发证机关" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="许可证初次发证日期" prop="permitIssueDate">
              <el-date-picker
                v-model="form.permitIssueDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="许可证本次发证日期" prop="permitStartDate">
              <el-date-picker
                v-model="form.permitStartDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                @change="dateChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="许可证有效截止日期" prop="permitEndDate">
              <el-date-picker
                v-model="form.permitEndDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                :picker-options="pickerOptions"
                @change="endateChange"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-no-more-click type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listPermitIssue, getPermitIssue, delPermitIssue, addPermitIssue, updatePermitIssue } from '@/api/bhxq/enterprisePermitIssue'
// import { listEnterpriseInfoDropdown } from '@/api/bhxq/enterpriseInfo'

import {
  addCompanyPermit,
  delPermitIssue,
  getPermitIssue,
  listPermitIssue,
  updatePermitIssue
} from '@/api/informationReport'
import moment from 'moment'

export default {
  name: 'Index',
  dicts: ['bhxq_enterprise_permit_issue'],
  data() {
    const that = this
    return {
      // 许可证名称字典
      permitList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业许可证表格数据
      tableList: [{}],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // businessType: '1',
        permitIssueName: null,
        permitIssueDate: null,
        permitIssueCode: null
      },
      // 表单参数
      form: {
        // businessType: '1'
        permitStartDate: '',
        permitEndDate: ''
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < moment(that.form.permitStartDate).valueOf()
        }
      },
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: '企业名称不能为空', trigger: 'blur' }
        ],
        permitIssueName: [
          { required: true, message: '许可证名称不能为空', trigger: 'change' }
        ],
        permitIssueCode: [
          { required: true, message: '许可证编号不能为空', trigger: 'blur' }
        ],
        permitIssueOrg: [
          { required: true, message: '发证机关不能为空', trigger: 'blur' }
        ],
        permitIssueDate: [
          { required: true, message: '许可证初次发证日期不能为空', trigger: 'blur' }
        ],
        permitStartDate: [
          { required: true, message: '许可证本次发证日期不能为空', trigger: 'blur' }
        ],
        permitEndDate: [
          { required: true, message: '许可证有效截止日期不能为空', trigger: 'blur' }
        ]
      },
      // 日期范围
      dateRange: [],
      enterpriseList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dateChange() {
      console.log(new Date(this.form.permitEndDate).getTime())
      if (new Date(this.form.permitEndDate).getTime()) {
        this.form.permitEndDate = ''
      }
      this.$forceUpdate()
    },
    endateChange() {
      this.$forceUpdate()
    },
    /** 查询企业许可证列表 */
    getList() {
      if (this.dateRange == null) {
        this.dateRange = []
      }
      this.loading = true
      const data = {
        permitIssueName: this.queryParams.permitIssueName,
        permitIssueCode: this.queryParams.permitIssueCode,
        permitIssueDateStart: this.dateRange.length === 0 ? undefined : this.dateRange[0],
        permitIssueDateEnd: this.dateRange.length === 0 ? undefined : this.dateRange[1],
        pageNum: this.queryParams.pageNum,
        pageSize: this.queryParams.pageSize
      }
      listPermitIssue(data).then(response => {
        this.tableList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        // businessType: '1'
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增企业营运证件'
      // this.listEnterprise()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()

      const id = row.id || this.ids

      // this.listEnterprise()

      getPermitIssue(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改企业许可证'
      })
    },
    /** 提交按钮 */
    submitForm() {
      console.log('提交按钮后的form：', this.form)
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePermitIssue(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCompanyPermit(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除企业许可证编号为"' + row.permitIssueCode + '"的数据项？').then(function() {
        return delPermitIssue(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // this.download('background/bhxqEnterprisePermitIssue/export', {
      //   ...this.queryParams
      // }, `企业许可证_${new Date().getTime()}.xlsx`)
    }
    // 指定企业名称
    // listEnterprise() {
    //   listEnterpriseInfoDropdown({ businessType: '1' }).then(response => {
    //     this.enterpriseList = response.data
    //   })
    // }
  }
}
</script>
<style scoped>
.el-date-editor.el-input, .el-date-editor.el-input__inner {
  width: 100%;
}
</style>
